# -*- coding: utf-8 -*-
"""
-------------------------------------------------------------------------------
File    : yaml_handler.py
Time    : 2023/8/28 13:33
author  : mojin
Email   : 397135766@qq.com
-------------------------------------------------------------------------------
"""

import yaml
import json


class YAMLHandler:
    @staticmethod
    def read_yaml(file_path):
        with open(file_path, 'r', encoding='utf-8') as file:
            data = yaml.load(file, Loader=yaml.Loader)
        return data

    @staticmethod
    def write_yaml(file_path, data):
        with open(file_path, 'w', encoding='utf-8') as file:
            json_data = json.dumps(data, ensure_ascii=False).encode('unicode_escape').decode('utf-8')
            yaml.dump(json.loads(json_data), file, allow_unicode=True)


# # 写入YAML文件
# new_data = {
#     'key1': '啊沙发上',
#     'key2': {
#         'nested_key': 'new_nee'
#     },
#     'key22': {
#         'nested_key': 'new_4325345nee'
#     }
# }
# YAMLHandler.write_yaml('new_example.yaml', new_data)
#
# data = YAMLHandler.read_yaml('new_example.yaml')
# print(data)